package com.kamloo.stg.data.db;

import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ScheduleDAO {
	
	private SQLiteDatabase m_database;
	private MySQLiteHelper m_dbHelper;
	public static final String TABLE_NAME 	 		= "schedule";
	public static final String COLUMN_ID 		 	= "_id";
	public static final String COLUMN_FK_NODE_ID	= "fk_nodeId";
	public static final String COLUMN_FK_PERIOD_ID	= "fk_periodId";
	public static final String COLUMN_HOUR			= "hour";
	public static final String COLUMN_MINUTE		= "minute";
	private String[] m_allColumns = { COLUMN_ID, COLUMN_FK_NODE_ID, COLUMN_FK_PERIOD_ID,
									  COLUMN_HOUR, COLUMN_MINUTE};
	
	public static final String TABLE_CREATE = 
			"CREATE TABLE " + 
		    TABLE_NAME + "(" + 
		    COLUMN_ID + " INTEGER PRIMARY KEY, " + 
		    COLUMN_FK_NODE_ID + " INTEGER REFERENCES " + NodeDAO.TABLE_NAME + "(" + NodeDAO.COLUMN_ID + ") " +
		    		"ON UPDATE CASCADE ON DELETE CASCADE," + 
		    COLUMN_FK_PERIOD_ID + " INTEGER REFERENCES " + PeriodDAO.TABLE_NAME + "(" + PeriodDAO.COLUMN_ID + ") " +
		    		"ON UPDATE CASCADE ON DELETE CASCADE," + 
		    COLUMN_HOUR + " INTEGER," +
		    COLUMN_MINUTE + " INTEGER);";
	
	public ScheduleDAO(Context context) {
		m_dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		m_database = m_dbHelper.getWritableDatabase();
	}

	public void close() {
		m_dbHelper.close();
	}
	
	
	public void add(Schedule schedule) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, schedule.getId());
		values.put(COLUMN_FK_NODE_ID, schedule.getFk_nodeId());
		values.put(COLUMN_FK_PERIOD_ID, schedule.getFk_periodId());
		values.put(COLUMN_HOUR, schedule.getHour());
		values.put(COLUMN_MINUTE, schedule.getMinute());
		m_database.insert(TABLE_NAME, null, values);
	}
	
	public void delete(int id) {
		System.out.println("Schedule deleted with id: " + id);
		m_database.delete(TABLE_NAME, COLUMN_ID + " = " + id, null);
	}
	
	public void update(Schedule schedule) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_FK_NODE_ID, schedule.getFk_nodeId());
		values.put(COLUMN_FK_PERIOD_ID, schedule.getFk_periodId());
		values.put(COLUMN_HOUR, schedule.getHour());
		values.put(COLUMN_MINUTE, schedule.getMinute());
		m_database.update(TABLE_NAME, values, COLUMN_ID + " = " + schedule.getId(), null);
	}	
	
	private Schedule cursorToSchedule(Cursor cursor) {
		return new Schedule(cursor.getInt(0),
							cursor.getInt(1),
							cursor.getInt(2),
							cursor.getInt(3),
							cursor.getInt(4));
	}

	public Schedule select(int id) {
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, COLUMN_ID + " = " + id, 
									  null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Schedule newSchedule = cursorToSchedule(cur);
		cur.close();
		return newSchedule;
	}
	
	public ArrayList<Schedule> select(int fk_nodeId, int fk_periodId){
		ArrayList<Schedule> schedules = new ArrayList<Schedule>();
		
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
				  					  COLUMN_FK_NODE_ID + " = " + fk_nodeId + " AND " +
				  					  COLUMN_FK_PERIOD_ID + " = " + fk_periodId, 
				  					  null, null, null, null);

		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				schedules.add(cursorToSchedule(cur));
				cur.moveToNext();
			}
		}

		cur.close();
		return schedules;
	}	
}
